const express = require("express");
const router = express.Router();
const multer = require("multer");
const path = require("path");
const crypto = require("crypto");
const { verifyToken } = require("../middleware/jwt");
// MIME 类型到扩展名的映射表
const mimeToExt = {
  "image/jpeg": ".jpg",
  "image/png": ".png",
  "image/gif": ".gif",
  // 添加其他 MIME 类型和扩展名的映射
  // ...
  "application/pdf": ".pdf", // 示例：PDF 文件
  // ...
};

// 存储配置 本地上传
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "public/uploads"); // 上传文件的存储目录
  },
  filename: function (req, file, cb) {
    // 生成唯一的文件名
    const uniqueSuffix = crypto.randomBytes(16).toString("hex");
    // 根据 MIME 类型获取扩展名
    const ext = mimeToExt[file.mimetype] || ".unknown"; // 如果没有匹配的 MIME 类型，则使用 '.unknown'
    // 构造最终的文件名
    const filename = `${uniqueSuffix}${ext}`;
    cb(null, filename);
  },
});
// 存储配置 云上传
const storageOSS = multer.memoryStorage();

// 创建 Multer 实例
const upload = multer({ storage: storage });
const uploadOSS = multer({ storage: storageOSS });
const {
  uploadContorller,
  uploadOSSContorller,
  ssrRenderPage,
} = require("../contorller/utileContorller");
router.post("/upload", upload.single("file"), uploadContorller);
router.post(
  "/uploadOSS",
  uploadOSS.array("file", 12),
  uploadOSSContorller
);
router.get("/page/:idAndUuid", ssrRenderPage);
module.exports = router;
